package com.diyizu.youdianda.controller;

import com.diyizu.youdianda.common.R;
import com.diyizu.youdianda.dto.YddCateDto;
import com.diyizu.youdianda.entity.YddCate;
import com.diyizu.youdianda.service.YddCateService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;

/**
 * 内容管理-分类管理
 */
@Slf4j
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {

    @Autowired
    private YddCateService yddCateService;

    //分类列表-分页
    @GetMapping("/list")
    public R page(String catename, Integer page, Integer limit){
        log.info("分类列表请求：catename="+catename+";page="+page+";limit="+limit);
        YddCateDto yddCateDto = yddCateService.all(catename, page, limit);
        return R.success(yddCateDto);
    }

    //分类列表-详情
    @GetMapping("/rowInfo")
    public R<YddCate> rowInfo(Integer id){
        log.info("分类列表详情请求：id="+id);
        YddCate byId = yddCateService.getById(id);
        return R.success(byId);
    }

    //分类列表-添加
    @PostMapping("/add")
    public R<String> add(@RequestBody YddCate yddCate){
        log.info("分类列表添加请求：yddCate="+yddCate);
        boolean save = yddCateService.save(yddCate);
        if (save){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    //分类列表-修改
    @PostMapping("/update")
    public R<String> update(@RequestBody YddCate yddCate){
        log.info("分类列表修改请求：yddCate="+yddCate);
        boolean b = yddCateService.updateById(yddCate);
        if (b){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    //分类列表-全部
    @GetMapping("/all")
    public R<List<YddCate>> all(){
        log.info("分类列表全部请求。。。");
        List<YddCate> list = yddCateService.list();
        return R.success(list);
    }

    //分类列表-删除
    @GetMapping("/del")
    public R<String> del(Integer id){
        log.info("分类列表删除请求：id="+id);
        boolean b = yddCateService.removeById(id);
        if (b){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }



}
